package com.omiis.wechat.performance_test2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class VUser implements Runnable {

	private static Logger logger = LoggerFactory.getLogger(VUser.class);

	private Map<String, String> result = new HashMap<String, String>();
	private String userId = "";
	private String conversationId = "";
	private String senderRole = "CUSTOMER";
	private String senderId = "";
	private String channelId = "";

	public String getConversationId() {
		return conversationId;
	}

	public void setConversationId(String conversationId) {
		this.conversationId = conversationId;
	}

	public String getSenderRole() {
		return senderRole;
	}

	public void setSenderRole(String senderRole) {
		this.senderRole = senderRole;
	}

	public String getSenderId() {
		return senderId;
	}

	public void setSenderId(String senderId) {
		this.senderId = senderId;
	}

	private List<String> txtMessages = new ArrayList<>();

	public VUser(String userId, String senderRole) {
		this.setUserId(userId);
		this.setSenderId(getUserId());
		this.setSenderRole(senderRole);
	}

	public VUser(String userId) {
		this.setUserId(userId);
		this.setSenderId(getUserId());
		this.setConversationId(UUID.randomUUID().toString());
		this.setSenderRole("CUSTOMER");
		this.setChannelId("WECHAT");
		this.setTxtMessages(MessageUtil.getTxtMessages());
	}

	@Override
	public void run() {
		logger.info("开始测试，微信用户：{}。", getUserId());
		IOmiisMessager omiis = new OmiisMessager();
		getResult().put("status", "");
		OmiisResponseMessageHandler handler = new OmiisResponseMessageHandler(this);
		omiis.registerMessageListener(handler);
		omiis.connect();
		// try {
		// Thread.currentThread().sleep(10 * 1000);
		// } catch (InterruptedException e) {
		// e.printStackTrace();
		// }
		if ("CUSTOMER".equals(getSenderRole())) {
			boolean wait = false;
			int limit = 5;
			int i = 0;
			do {
				try {
					synchronized (getResult()) {
						getResult().wait(30 * 1000);
					}
				} catch (InterruptedException e) {
					logger.error(e.getMessage(), e);
				}
				i++;
				wait = StringUtils.isEmpty(getResult().get("status"));
			} while (wait && i <= limit);
			logger.info("VUser主动断开连接。");
			omiis.disconnect();
		}
		logger.info("微信用户{}，通讯完成，退出。", getUserId());
	}

	/**
	 * @return the userId
	 */
	public String getUserId() {
		return userId;
	}

	/**
	 * @param userId
	 *            the userId to set
	 */
	public void setUserId(String userId) {
		this.userId = userId;
	}

	/**
	 * @return the channelId
	 */
	public String getChannelId() {
		return channelId;
	}

	/**
	 * @param channelId
	 *            the channelId to set
	 */
	public void setChannelId(String channelId) {
		this.channelId = channelId;
	}

	/**
	 * @return the txtMessages
	 */
	public List<String> getTxtMessages() {
		return txtMessages;
	}

	/**
	 * @param txtMessages
	 *            the txtMessages to set
	 */
	public void setTxtMessages(List<String> txtMessages) {
		this.txtMessages = txtMessages;
	}

	/**
	 * @return the result
	 */
	public Map<String, String> getResult() {
		return result;
	}

	/**
	 * @param result
	 *            the result to set
	 */
	public void setResult(Map<String, String> result) {
		this.result = result;
	}
}
